package com.situ.water_pay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.util.GetUUID;
import com.situ.water_Invoice.mapper.IvInvoiceMapper;
import com.situ.water_Invoice.pojo.IvInvoice;
import com.situ.water_pay.mapper.*;
import com.situ.water_pay.pojo.*;
import com.situ.water_pay.service.PyBillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.water_pay.vo.FundRecovery;
import com.situ.water_user.mapper.UsUserMapper;
import com.situ.water_user.pojo.UsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Angel
 * @since 2020-12-01
 */
@Service
public class PyBillServiceImpl extends ServiceImpl<PyBillMapper, PyBill> implements PyBillService {
    @Autowired
    private PyBillMapper billMapper;
    @Autowired
    private UsUserMapper userMapper;
    @Autowired
    private PyBilldetailMapper pyBilldetailMapper;
    @Autowired
    private PyPayMapper payMapper;
    @Autowired
    private PyBalanceMapper pyBalanceMapper;
    @Autowired
    private PyBalancedetailMapper pyBalancedetailMapper;
    @Autowired
    private PyUserhistoryMapper userhistoryMapper;
    @Autowired
    private IvInvoiceMapper ivInvoiceMapper;

    /**
     * 缴费
     *
     * @param pyBill
     * @throws Exception
     */
    @Transactional
    public void payment(PyBill pyBill, Integer empId) throws Exception {
        //查询用户余额
        UsUser usUser = userMapper.selectById(pyBill.getUserNo());
        //查询用户所有未结清的交费单
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("UserNo", pyBill.getUserNo());
        List<PyBill> PyBillList = billMapper.selectList(wrapper);
        //计算应交金额   未结清水费单应交+用户余额
        BigDecimal due = new BigDecimal(Double.toString(0.0));
        for (PyBill bill : PyBillList) {
            due = due.add(new BigDecimal(Double.toString(bill.getBillMoney())));
        }
        //销账前实收
        double due2 = due.doubleValue();
        if (usUser.getUserMoney() > 0) {
            //应交-用户余额
            due = due.subtract(new BigDecimal(Double.toString(usUser.getUserMoney())));
        }
        if (usUser.getUserMoney() < 0) {

            due = due.add(new BigDecimal(Double.toString(usUser.getUserMoney())));
        }
        //计算找零金额  实交  -  应交
        BigDecimal change = new BigDecimal(Double.toString(0.0));
        change = new BigDecimal(Double.toString(pyBill.getRealMoney())).subtract(due);
        //交费单
        PyPay pyPay = new PyPay();
        String payNo = GetUUID.GetPayNo();
        pyPay.setPayNo(payNo);
        pyPay.setPayType("坐收");
        pyPay.setPayDate(new Date());
        pyPay.setUserNo(usUser.getUserno());
        pyPay.setInvoice(pyBill.getInvoiceNo());
        pyPay.setPayMoney(pyBill.getRealMoney());
        BigDecimal bigDecimal = new BigDecimal(Double.toString(pyBill.getRealMoney())).subtract(change);
        pyPay.setUseMoney(bigDecimal.doubleValue());
        pyPay.setEmpID(empId);
        payMapper.insert(pyPay);
        //更新水费单  及水费单详情数据
        for (PyBill bil : PyBillList) {
            bil.setRealMoney(bil.getBillMoney());
            bil.setBalanceDate(new Date());
            bil.setBalance(true);
            billMapper.updateById(bil);
            //销账表
            PyBalance pyBalance = new PyBalance();
            pyBalance.setUserNo(usUser.getUserno());
            pyBalance.setBalanceDate(new Date());
            pyBalance.setBalanceMoney(due.doubleValue());
            pyBalance.setBillNo(bil.getBillNo());
            pyBalance.setBillRealMoney2(due2);
            pyBalance.setBillRealMoney2(due.doubleValue());
            pyBalance.setPayUseMoney1(usUser.getUserMoney());
            pyBalance.setPayUseMoney2(change.doubleValue());
            pyBalance.setPayNo(payNo);
            pyBalanceMapper.insert(pyBalance);
            //更新水费单详情
            QueryWrapper queryWrapper = new QueryWrapper();
            wrapper.eq("BillNo", bil.getBillNo());
            List<PyBilldetail> pyBilldetailList = pyBilldetailMapper.selectList(wrapper);
            for (PyBilldetail billdetail : pyBilldetailList) {
                billdetail.setRealMoney(billdetail.getDetailMoney());
                billdetail.setBalanceDate(new Date());
                billdetail.setBalance(true);
                pyBilldetailMapper.updateById(billdetail);
                //销账明细表
                PyBalancedetail pyBalancedetail = new PyBalancedetail();
                pyBalancedetail.setParentID(pyBalance.getId());
                pyBalancedetail.setUserNo(usUser.getUserno());
                pyBalancedetail.setBalanceDate(new Date());
                pyBalancedetail.setBalanceMoney(billdetail.getDetailMoney());
                pyBalancedetail.setBillNo(bil.getBillNo());
                pyBalancedetail.setDetailRealMoney1(due2);
                pyBalancedetail.setDetailRealMoney2(due.doubleValue());
                pyBalancedetail.setPayUseMoney1(usUser.getUserMoney());
                pyBalancedetail.setPayUseMoney2(change.doubleValue());
                pyBalancedetail.setBillDetailID(billdetail.getId());
                pyBalancedetail.setCostTypeCode(billdetail.getCostTypeCode());
                pyBalancedetail.setPayNo(payNo);
                pyBalancedetailMapper.insert(pyBalancedetail);
            }
        }
        //用户余额更新
        usUser.setUserMoney(change.doubleValue());
        userMapper.updateById(usUser);
        //用户历史详单
        PyUserhistory pyUserhistory = new PyUserhistory();
        pyUserhistory.setUserNo(usUser.getUserno());
        pyUserhistory.setTypee(1);
        pyUserhistory.setOrderNo(payNo);
        pyUserhistory.setDatee(new Date());
        pyUserhistory.setBillMoney(due.doubleValue());
        pyUserhistory.setPayMoney(pyBill.getRealMoney());
        pyUserhistory.setUserMoney(change.doubleValue());
        userhistoryMapper.insert(pyUserhistory);
        //发票
        IvInvoice invoice = ivInvoiceMapper.selectById(Integer.valueOf(pyBill.getInvoiceNo()));
        invoice.setUsed(true);
        invoice.setEmpID(empId);
        invoice.setUseDate(new Date());
        ivInvoiceMapper.updateById(invoice);
        System.out.println("**************************交费成功****************************");
    }

    @Override
    public Date getMaxDate() {
        return billMapper.getMaxDate();
    }

    @Override
    public Date getMinDate() {
        return billMapper.getMinDate();
    }

    @Override
    public List<FundRecovery> getFundRecovery(String yaer) {
        return billMapper.getFundRecovery(yaer);
    }

    @Override
    public List<FundRecovery> getUserProjectedFees(Integer amount) {
        return billMapper.getUserProjectedFees(amount);
    }

    @Override
    public IPage<PyBill> getUserArrears(Page<PyBill> billPage, String[] volumes, String minDate, String maxDate, String sortField) {
        return billMapper.getUserArrears(billPage,volumes,minDate,maxDate,sortField);
    }
}
